<?php

class GTL_Action extends Zend_Controller_Action {

    public function init() {

        $this->initView();
        $this->config = Zend_Registry::get('config');
        $PAGING_PER_PAGE = $this->config->PAGING_PER_PAGE;
        $request = $this->getRequest();
        $controllerName = $request->getControllerName();
        $moduleName = $request->getModuleName();
        $action = $request->getActionName();

        $auth = Zend_Auth::getInstance();
        $storage = new Zend_Auth_Storage_Session(Zend_Registry::get('sessionName'));
        $auth->setStorage($storage);
        
        $this->view->user = $auth->getIdentity();
        $this->USER = $auth->getIdentity();
        
        
        $metaDataModel = new Model_Metadata();
        
        
        
        //Language logic
        if ($this->USER) {
            $PAGING_PER_PAGE = $this->USER->PAGING_PER_PAGE;

            //If user has set personal preference use that language
            if ($this->USER->usr_lang_code) {
                $lang_code = $this->USER->usr_lang_code;
                //$this->USER->language = $this->USER->usr_lang_code;
            } else {
                // default language set by Admin 
                //*default language id  **/
                $ADMIN_DEFAULT_LANGUAGE = $metaDataModel->fetchEntryByKey("DEFAULT_LANGUAGE");
                $ADMIN_LANGUAGE_ID = $ADMIN_DEFAULT_LANGUAGE['mtd_value'];
                $languageModel = new Model_Language();
                $where = 'lang_id=' . $ADMIN_LANGUAGE_ID;
                $languagedefaultArray = $languageModel->fetchType($where);
                $default_lang_code = strtolower($languagedefaultArray[0]['lang_code']);
            }
        } else {
            //Get from  Application.ini default english language 
            $lang_code = $this->config->DEFAULT_LANGUAGE;
        }

        if ($lang_code) {
            // echo $lang_code;exit;
            $langPath = APPLICATION_PATH . DIRECTORY_SEPARATOR . 'language' . DIRECTORY_SEPARATOR . $lang_code . DIRECTORY_SEPARATOR;
            // Create an instance of Zend's gettext adapter
            $translate = new Zend_Translate_Adapter_Gettext($langPath . 'messages.mo', $lang_code);
            $translate->setLocale($lang_code);
            // Set this Translation as global translation for the view helper
            Zend_Registry::set('Zend_Translate', $translate);
        } else {
            $langPath = APPLICATION_PATH . DIRECTORY_SEPARATOR . 'language' . DIRECTORY_SEPARATOR . $default_lang_code . DIRECTORY_SEPARATOR;
            // Create an instance of Zend's gettext adapter
            $translate = new Zend_Translate_Adapter_Gettext($langPath . 'messages.mo', $default_lang_code);
            $translate->setLocale($default_lang_code);
            // Set this Translation as global translation for the view helper
            Zend_Registry::set('Zend_Translate', $translate);
        }
        if ($this->USER) {
           // echo $this->USER->role."=====".$this->USER->COMPANY_SESSION_ID;exit;
            if ($this->USER->role == 'CONTRACTOR' && $this->USER->COMPANY_SESSION_ID == '') {
                //logic for contractor company
                $contructor_department = new Model_Departmentcontractor();
                $contructorDepartmentData = $contructor_department->fetchSITEByContractor($this->USER->usr_id);
                $SITEId = implode(",",$contructorDepartmentData);
                //echo $DepartmentId;exit;
                
                $siteDepartment = new Model_Department();
                $companyData = $siteDepartment->getsitecompanyArray($SITEId);
                $this->view->companyData = $companyData;
            }
        }
        
        Zend_Registry::set('Zend_Translate', $translate);
        $this->ZFTRANSLATE = Zend_Registry::get('Zend_Translate');
        $ADMIN_HEADER_LOGO= $metaDataModel->fetchEntryByKey("HEADER_LOGO");
        $ADMIN_HEADER = $ADMIN_HEADER_LOGO['mtd_value'];
        $this->view->LOGO_NAME = $ADMIN_HEADER;
        $this->view->LOGO = $this->config->PUBLIC_PATH."upload/logo/".$ADMIN_HEADER;
        $this->view->SITE_NAME = $this->config->SITE_NAME;
        $this->view->IMAGE_PATH = $this->config->IMAGE_PATH;
        $this->view->BASE_URL = $this->config->BASE_URL;
        $this->view->FLIP_PATH = $this->config->FLIP_PATH;
        $this->view->JS_PATH = $this->config->JS_PATH;
        $this->view->CSS_PATH = $this->config->CSS_PATH;
        //$this->view->CSS_PATH = $this->config->CSS_PATH;
        $this->view->LATITUDE = $this->config->LATITUDE;
        $this->view->LONGITUDE = $this->config->LONGITUDE;

        $this->PAGING_PER_PAGE = $PAGING_PER_PAGE;


        $this->view->PUBLIC_PATH = $this->config->PUBLIC_PATH;
        $this->view->UPLOAD_PATH = $this->config->UPLOAD_PATH;
        $this->view->MODULE_NAME = $moduleName;
        $this->view->CONTROLLER_NAME = $controllerName;
        $this->view->ACTION_NAME = $action;
        $this->view->request = $this->request = $this->getRequest();

        $this->_flashMessenger = $this->_helper->getHelper('FlashMessenger');
    }

    /**
     * preDispatch method to check user authentication.
     * if user is not logged in , will dispaly login screen.
     * 
     * @return  void
     * 
     */
    public function preDispatch() {

        $request = $this->getRequest();
        $moduleName = $request->getModuleName();
        $controllerName = $request->getControllerName();
        $action = $request->getActionName();
        $this->config = Zend_Registry::get('config');

        if ('default' == $moduleName) {
            $auth = Zend_Auth::getInstance();

            if (!$auth->hasIdentity() && $controllerName != 'login') {
                $this->_redirect('/login');
            } else {
                return true;
            }
        }
    }

    /*
     * To display success message.
     */

    protected function addSuccessMessage($message) {
        #$this->_helper->_flashMessenger->addMessage(array('message' => $message, 'status' => 'successFlash'));
        if (is_array($message) && count($message) > 0) {
            foreach ($message as $val) {
                $this->_helper->_flashMessenger->addMessage(array('message' => $val, 'status' => 'successFlash'));
            }
        } else {
            $this->_helper->_flashMessenger->addMessage(array('message' => $message, 'status' => 'successFlash'));
        }
    }

    /*
     * To display warning message.
     */

    protected function addWarningMessage($message) {
        $this->_helper->_flashMessenger->addMessage(array('message' => $message, 'status' => 'warningFlash'));
    }

    /*
     * To display error message.
     */

    protected function addErrorMessage($message) {
        if (is_array($message) && count($message) > 0) {
            foreach ($message as $val) {
                $this->_helper->_flashMessenger->addMessage(array('message' => $val, 'status' => 'errorFlash'));
            }
        } else {
            $this->_helper->_flashMessenger->addMessage(array('message' => $message, 'status' => 'errorFlash'));
        }
    }

    //session unset for search
    public function list_sess_unset($controller_name) {
        if ($_SESSION) {
            if (array_key_exists('_LISTING_SESS', $_SESSION)) {
                if(isset($_SESSION['_LISTING_SESS'][$controller_name])){
                    $controllerSess = $_SESSION['_LISTING_SESS'][$controller_name];
                }else{
                    $controllerSess = "";
                }
                unset($_SESSION['_LISTING_SESS']);
                $_SESSION['_LISTING_SESS'][$controller_name] = $controllerSess;
                
            }
        }
    }

}

